var WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
          indicatorDots: false,
          autoplay: true,
          interval: 5000,
          duration: 1000,
          showTypes:false,
          num:1,
          marketNum:0,
          animationClass:''
           
        
    },
    onLoad:function(opt){

      getApp().getUserInfo().then(res=>{
     
       this.setData({
        userInfo:res,
       })  

       this.setData({
            goodsId:opt.id
        })
        this.getDetail();
        this.getNum();

       
       // this.getAllSheBeiForTuiJian(address.province);
     })

       // var userInfo= wx.getStorageSync('userInfo');
       //  this.setData({
       //      userInfo:userInfo
       //  })
        
    },
    getNum:function(){
       var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/shopping/find/num?openid='+this.data.userInfo.openid,
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                  marketNum:res.data.obj
                })
              }
            console.log(res)  
            
         }
       })
    },
    getDetail:function(id){
       var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/parts/getDetails?id='+this.data.goodsId,
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                    goodsInfo:res.data.obj
                })
              }

            var  article=res.data.obj.content;
           
            WxParse.wxParse('article', 'html', article, _self, 5);
          
         }
       })  
    },
    onShow:function(){
       
    },
    showTypes(){
      this.setData({
        showTypes:true
      })
    },
    chooseType:function(e){
      this.setData({
       selectTypeId:e.currentTarget.dataset.id,
       selectTypeName:e.currentTarget.dataset.name,
       selectMoney:e.currentTarget.dataset.money
      })
    },
    makeSure:function(){
       if (!this.data.selectTypeId&&this.data.goodsInfo.type==2) {

        wx.showToast({
           title: '请选择商品规格',
           icon:'none',
           duration: 2000
         })
        return
       }
       this.setData({
         showTypes:false
       })
       this.addMarket();

    },
    onMyEvent:function(e){
        console.log(e.detail)
        // this.setData({
        //     item:e.detail
        // })
    },
    addMarket1:function(){
       this.setData({
         showTypes:true
        })
       
    },

    addMarket:function(){
       
        var _self=this;
        if (this.data.goodsInfo.normslist&&!this.data.selectTypeId) {
            wx.showToast({
              title: '请选择商品规格',
              icon:'none',
              duration: 2000
            })

        }else{
            var json={
                openid:this.data.userInfo.openid,
                 "id":this.data.goodsInfo.id,//配件id
                  "merchantId":this.data.goodsInfo.merchantId,//供应商ID
                  "type":this.data.goodsInfo.type,//商品类型：1普通商品 2规格商品,
                  "normsId":this.data.selectTypeId,//规格ID（如果type==1,就不需要传）,
                  "num":this.data.num,//数量 
                  "status":0//写死0  添加购物都是不勾选状态
                
            }
            wx.request({
            url: getApp().globalData.baseUrl + '/pj/shopping/add',
            method:"POST",
            data:json,
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              console.log('成功')
               console.log(res)
                wx.showToast({
                title: '添加成功，点击购物车下单',
                icon:'none',
                duration: 2000
              })

               _self.setData({
                animationClass:'amimation'
               })
               var timer=setTimeout(function(){
                  _self.setData({
                    animationClass:''
                   })
                  clearTimeout(timer)

               },1000)
               _self.getNum();
                // wx.setStorageSync('chooseProvince5',null);
                // wx.setStorageSync('chooseCity5',null);
                // wx.switchTab({
                //   url: '../../pages/mine/mine'
                // })
                //  wx.navigateTo({
                //   url: '/pages/shopMarket/shopMarket'
                // })
                               
            }
            }) 
        }
       

    },
    addAction:function(){
      var num=this.data.num;
      num++
      this.setData({
        num:num
      })
    },
    changeNum:function(e){
       console.log(e.detail.value)
       this.setData({
        num:e.detail.value
      })
    },
    subAction:function(){
       var num=this.data.num;
       if (num>1) {
        num--
       }else{
        num=1
       }
       this.setData({
        num:num
      })

    },
    turnPage:function(){
      var userInfo= wx.getStorageSync('userInfo');
      if (userInfo.phone==null||userInfo.phone.length!=11) {
         this.setData({
         showToast:true
        })
      }else{
         wx.switchTab({
          url: '/pages/shopMarket/shopMarket'
         })
      }
    },
    callPhone:function(){
      wx.makePhoneCall({
        phoneNumber: this.data.goodsInfo.phone//仅为示例，并非真实的电话号码
      })
    },
    hideView:function(){
       this.setData({
         showTypes:false
       })
    },
    onCannel:function () {
     this.setData({
       showToast:false
     })
    },
    onLoginSuccess:function () {
       this.setData({
       showToast:false
      })
    },
    onShareAppMessage:function(){
        return {
              title: this.data.goodsInfo.name,
              path: '/pages/goodsDetail/goodsDetail?id='+this.data.goodsId+"&type=share"
        }
    },
    onShareTimeline:function(){
        return {
              title: this.data.goodsInfo.name,

              query:'id='+this.data.goodsId+"&type=share",
              path: '/pages/goodsDetail/goodsDetail?id='+this.data.goodsId+"&type=share"

        }
    },
    
});